package edu.netjava.Queue;

/**
 * the class main function is that
 * it can increase with its adding action 
 */

public class ArrayIncrease {
	

	Object objects[];
	int size = 10;
	int index = -1;
	float pre = .2f;
	
	public ArrayIncrease(){
		objects = new Object[size];
	}
	
	/**
	 * @param obj  the element add into the array
	 */
	 public void add(Object obj){
		 index++;
		 if(index >= size){
			 size += size*pre;
			 Object[] temp = new Object[size];
			 System.arraycopy(objects, 0, temp, 0, objects.length);
			 temp[index]=obj;
			 objects = temp;
		 }
		 objects[index]=obj;
	 }
	 
	 /**
	  * 
	  * @param index the location of the element needed to delete
	  */
	 public void delete(int index){
		 if(index<0 || index>size){
				System.out.println("the range of index is illegel ");
			}
			int deleNum = size - index -1;
			System.arraycopy(objects, index+1, objects, index, deleNum);
	 }
	 
	 /**
	  * 
	  * @param index the location of the element to get
	  * @return the element wanted
	  */
	 public Object get(int index){
		 return objects[index];
	 }
	 
	 /**
	  * 
	  * @param index: the location  to insert
	  * @param obj  : the element
	  */
	 public void add(int index,Object obj){
		 index++;
		 if(index<0){
			 System.out.println("the index is out of Bounds!");
		 }
		 if(index > size){
			 size += size*pre;
		 }
		 int copylen = size - index;
		 System.arraycopy(objects, index, objects, index+1, copylen);
		 objects[index] = obj;
	 }
	 
	 /**
	  * 
	  * @return the size of array
	  */
	 public int size(){
		 for(int i=0;i<objects.length;i++){
			 if(objects[i] == null){
				 return i;
			 }
		 }
		 return this.size;
	 }
	 
	 /**
	  * print all elements in the array
	  */
	 public void printAll(){
		 for(int i=0;i<objects.length;i++){
			 output(objects[i]);
		 }
	 }
	 
	 /**
	  * print elements in the array
	  */
	 public void printExitE(){
		 for(int i=0;i<objects.length;i++){
			 if(objects[i]==null){
				 break;
			 }
			 output(objects[i]);
		 }
	 }
	 /**
	  * a method to deal print elements
	  */
	 public static void output(Object object){
		 System.out.println(object);
	 }

	 
}
